//
//  MBProgressHUD+XX.m
//
//  Created by Shaw
//

#import "MBProgressHUD+XX.h"

@implementation MBProgressHUD (XX)
#pragma mark 显示信息
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
{
    [MBProgressHUD hideHUD];
    
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.labelText = text;
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // 1秒之后再消失
    [hud hide:YES afterDelay:1.0];
}
+(void)showVoiceRecord:(BOOL)show{
    
    [MBProgressHUD hideHUD];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication].windows lastObject] animated:YES];
    
    UIImageView *recordImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"voiceRecord1"]];
    NSMutableArray *images = [NSMutableArray new];
    for (int i= 1; i<13; i++) {
        [images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"voiceRecord%i",i]]];
    }
    recordImageView.animationImages = images;
    recordImageView.animationDuration = 3;
    recordImageView.animationRepeatCount = 0;
    recordImageView.frame = CGRectMake(0, 0, 100, 50);
    [recordImageView startAnimating];
    if (show) {
        
        hud.customView = recordImageView;
        hud.mode = MBProgressHUDModeCustomView;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:55];
    }else{
        hud.hidden = YES;
    }
  
}

+(void)showCountdown:(BOOL)show{
    
    [MBProgressHUD hideHUD];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication].windows lastObject] animated:YES];
    
    UIImageView *countDownImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"countdown0"]];
    countDownImageView.contentMode = UIViewContentModeScaleAspectFit;
    countDownImageView.frame = CGRectMake(0, 0, 100, 50);
    NSMutableArray *images = [NSMutableArray new];
    for (int i=1; i<=5; i++) {
        [images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"countdown%i",i]]];
    }
    countDownImageView.animationImages = images;
    countDownImageView.animationDuration = 5;
    countDownImageView.animationRepeatCount = 1;
    [countDownImageView startAnimating];
    if (show) {
        hud.customView = countDownImageView;
        hud.mode = MBProgressHUDModeCustomView;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:5];
    }else{
        hud.hidden = YES;
    }
}

#pragma mark 显示错误信息
+ (void)showError:(NSString *)error toView:(UIView *)view{
    [self show:error icon:@"error.png" view:view];
}

+ (void)showSuccess:(NSString *)success toView:(UIView *)view
{
    [self show:success icon:@"success.png" view:view];
}

#pragma mark 显示一些信息

+ (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view{
    return [self showMessage:message toView:view hide:NO];
}

+ (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view hide:(BOOL)hide {
    [MBProgressHUD hideHUD];
    
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.labelText = message;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    // YES代表需要蒙版效果
    //hud.dimBackground = YES;
    
    if (hide) {
        // 1秒之后再消失
        [hud hide:YES afterDelay:1.0];
    }
    return hud;
}

+ (void)showSuccess:(NSString *)success
{
    [self showSuccess:success toView:nil];
}

+ (void)showError:(NSString *)error
{
    [self showError:error toView:nil];
}

+ (MBProgressHUD *)showMessage:(NSString *)message
{
    return [self showMessage:message toView:nil];
}
+ (MBProgressHUD *)showMessage:(NSString *)message hide:(BOOL)hide
{
    return [self showMessage:message toView:nil hide:hide];
}

+ (void)hideHUDForView:(UIView *)view
{
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    [self hideHUDForView:view animated:YES];
}

+ (void)hideHUD
{
    [self hideHUDForView:nil];
}
@end
